package com.bml.architect.framework.register;

import com.bml.architect.framework.Address;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddressRegister {

   private static Map<String, List<Address>> registerUrl = new HashMap<>();

    public static void registerAddress(String name,Address address){
        List<Address> addresses = registerUrl.get(name);
        if(addresses == null ){
            addresses = new ArrayList<>();
        }
        addresses.add(address);
        registerUrl.put(name,addresses);

        saveFile();
    }

    public static List<Address> getAddress(String name){
        Map<String, List<Address>> file = getFile();
        return file.get(name);
    }


    private static void saveFile() {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream("/temp.txt");
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(registerUrl);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static Map<String, List<Address>> getFile() {
        try {
            FileInputStream fileInputStream = new FileInputStream("/temp.txt");
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
            return (Map<String, List<Address>>) objectInputStream.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
